package cn.kgc.config;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;

public class CustomerGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //从请求头获取token
        String token=exchange.getRequest().getHeaders().getFirst("token");
        URI uri=exchange.getRequest().getURI();
        System.out.println("路径"+uri.toString());
        if (uri.toString().startsWith("http://localhost:7900/api/user/doLogin")){
            return chain.filter(exchange);
        }
        if (token!=null&&!"".equals(token)){
            //交给下一个梁桥
            return chain.filter(exchange);
        }
        //未授权的登录
        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
